package com.ikkong.core.intercept;

import javax.naming.NoPermissionException;

import com.fj.hiwetoptools.exception.ExceptionUtil;
import com.ikkong.common.vo.ShiroUser;
import com.ikkong.core.base.BaseController;
import com.ikkong.core.constant.Const;
import com.ikkong.core.constant.ConstShiro;
import com.ikkong.core.jfinal.ext.kit.JLogKit;
import com.ikkong.core.jfinal.ext.shiro.ShiroKit;
import com.ikkong.core.toolbox.Exceptions;
import com.ikkong.core.toolbox.log.LogManager;
import com.jfinal.aop.Invocation;
import com.jfinal.core.ActionException;
import com.jfinal.core.Controller;
import com.jfinal.kit.LogKit;
import com.jfinal.kit.StrKit;

/**
 * 全局异常处理
 * @author ikkong
 *
 */
public class ExceptionHandlerInterceptor extends BladeInterceptor {

	@Override
	public void intercept(Invocation inv) {
		try{
			inv.invoke();
		}catch(Exception ex){
			LogKit.error(ex.getMessage(),ex);
			Controller cont = inv.getController();
			if(cont instanceof BaseController){
                BaseController controller = (BaseController) inv.getController();
				String url = Const.error500Path;
				String msg = ex.getMessage();
                if (ex.getClass() == NoPermissionException.class) {
					url = Const.noPermissionPath;// 无权限抛出的异常
					msg = ConstShiro.NO_PERMISSION;
				}else if(ex.getClass() == ActionException.class){
					url = ((ActionException)ex).getErrorRender().getView();
				}
				try {
					if(StrKit.notBlank(msg)){
						ShiroUser user = ShiroKit.getUser();
						LogManager.doLog(user, msg+"-"+ (null!=ex ?Exceptions.getStackTraceAsString(ex):""), "异常日志", controller.getRequest(), false);
					}
				} catch (Exception logex) {
					LogKit.error("异常日志",logex);
				}
				//处理上传不是json请求，但是返回是json数据的，只有导入功能，单独处理
                if (controller.isAjax() || inv.getMethodName().equals("doImport")) {
					result.addFail(msg == null ? ex.getClass().getName():msg);
					controller.renderJson(result);
				} else {
					controller.setAttr("error", msg);
					controller.setAttr("class", ex.getClass());
					controller.setAttr("method", controller.getRequest().getRequestURI());
					controller.render(url);
				}
			}
		}
	}

}
